<?php
session_start();
session_register("codesess");
$width = "66";
$height = "16";
$len = "4";
$abc="2";
$bgcolor = "#EEEEEE";
$noise = true;
$noisenum = 100;
$border = true;
$bordercolor = "#666666";
$image = imageCreate($width, $height);
$back = getcolor($bgcolor);
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
$code="";
if($abc==1){
 for ($i=0; $i<$len; $i++)
{
  $randtext = rand(0, 9);
  $crd=rand(0,9);
  $code1=substr(md5($randtext),$crd,1);
  $code.=$code1;
  $textColor = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
  $randsize = rand($size-$size/10, $size+$size/10);
  $location = $left+($i*$size+$size/10)+4;
  imagestring($image, 5, $location, 1,  $code1, $textColor);
}
}
if($abc==2){
for ($i=0; $i<$len; $i++)
{
    $randtext = rand(0, 9);
    $code .= $randtext;
 $textColor = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
 $randsize = rand($size-$size/10, $size+$size/10);
 $location = $left+($i*$size+$size/10)+4;
 imagestring($image, 5, $location, 1,  $randtext, $textColor);
}
}
if($noise == true) setnoise();
$_SESSION["codesess"] = addslashes($code);
$bordercolor = getcolor($bordercolor); 
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
function getcolor($color)
{
     global $image;
     $color = eregi_replace ("^#","",$color);
     $r = $color[0].$color[1];
     $r = hexdec ($r);
     $b = $color[2].$color[3];
     $b = hexdec ($b);
     $g = $color[4].$color[5];
     $g = hexdec ($g);
     $color = imagecolorallocate ($image, $r, $b, $g); 
     return $color;
}
function setnoise()
{
 global $image, $width, $height, $back, $noisenum;
 for ($i=0; $i<$noisenum; $i++){
  $randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));  
  imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
 } 
}
?>